<?
header('Content-Type: text/html; charset=UTF-8');
require 'crawl.php';
require 'database.php';
$db = new DB();

$getNow = new DateTime('now');
$today = $getNow->format('Y-m-d');

$cat = isset($_GET['c']) ? strval($_GET['c']) : NULL;
$p = isset($_GET['p']) ? strval($_GET['p']) : 0;

$page = '';
if ($p)
	$page = '/sayfa-' . $p . '/';

$url = 'http://www.oyungemisi.com/' . $cat . $page;
// $url = 'http://www.oyuncuyuz.biz.tr/' . $cat . $page;

crawl2($url);

function crawl2($url) {
	global $db, $today, $cat;
	$array = $array_sql = array();

	$html = file_get_html($url);
// 	echo $html;

	if ($html->find('ul#games', 0))
		$string = $html->find('ul#games', 0)->innertext;
	else
		$string = '';

	if (!$string) {
		return false;
		$html->clear();
		unset($html);
	}

	$str = str_get_html($string);

	foreach ($str->find('li.boule') as $e)
		$e->outertext = '';
	foreach ($str->find('li#baguette') as $e)
		$e->outertext = '';

	foreach ($str->find('a') as $element){
		$plaintext = trim($element->plaintext);
		$plaintext = str_replace("'", '', $plaintext);
		$link = trim($element->href);

		$array['name'][] = $plaintext;
		$array['url'][] = $link;
	}

	foreach ($str->find('img') as $element){
		$img = trim($element->src);
		$array['image'][] = $img;
	}

// 	var_dump($array);
	$total = count($array['url']);

	$num = 0;
	for ($i = 0; $i < $total; $i++) {

		// check url exists
		$Qry = 'SELECT fix_url
				FROM games
				WHERE fix_url = "' . $array['url'][$i] . '"';

		if ($db->numRows($Qry) == 0) {
			++$num;

			$data = array(
					'name'=>$array['name'][$i],
					'name_seo'=>str_replace('/', '', $array['url'][$i]),
					'image'=>$array['image'][$i],
					'tags'=>trim($cat),
					'date'=>$today,
// 					'crawl_url'=>$array['url'][$i],
					'fix_url'=>$array['url'][$i]
			);

// 			var_dump($data);

			$newID = $db->insert('games', $data);

			if ($newID) {
				$des = array(
						'id'=>$newID
				);
				$db->insert('games_description', $des);
			}
		}
	}

	echo 'Total games added: ' . $num . '';
}

function crawl($url = '') {
	global $db, $today;

	$html = file_get_html($url);

//  echo $html;

	foreach ($html->find('div.sayfalama') as $e)
		$e->outertext = '';

	if ($html->find('div.oyunlar', 1))
		$string = $html->find('div.oyunlar', 1)->innertext;
	else
		$string = '';

	if (!$string) {
		return false;
		$html->clear();
		unset($html);
	}

	$str = str_get_html($string);

	$array = $array_sql = array();

	foreach ($str->find('a') as $element){
		$plaintext = trim($element->plaintext);
		$plaintext = str_replace("'", '', $plaintext);
		$link = trim($element->href);

		$array['name'][] = $plaintext;
		$array['url'][] = $link;
	}

	foreach ($str->find('img') as $element){
		$img = trim($element->src);
		$array['image'][] = $img;
	}

	$total = count($array['url']);

	$num = 0;
	for ($i = 0; $i < $total; $i++) {

		$link = str_replace('http://www.oyuncuyuz.biz.tr/', '', $array['url'][$i]);

		$arr = explode('/', $link);

		// check url exists
		$Qry = 'SELECT crawl_url
				FROM games
				WHERE crawl_url = "' . $array['url'][$i] . '"';

		if ($db->numRows($Qry) == 0) {
			++$num;

			$data = array(
					'name'=>$array['name'][$i],
					'name_seo'=>trim($arr[1]),
					'image'=>$array['image'][$i],
					'tags'=>trim($arr[0]),
					'date'=>$today,
					'crawl_url'=>$array['url'][$i]
			);

			$newID = $db->insert('games', $data);

			if ($newID) {
				$des = array(
						'id'=>$newID
				);
				$db->insert('games_description', $des);
			}
		}
	}

	echo 'Total games added: ' . $num . '';
}
